﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.DALFactory;

    public class CycleCountCriteriaBLL
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private static readonly ICycleCountCriteriaDA CycleCountCriteriaDA = DataAccess.CreateCycleCountCriteriaDA();
        private static readonly ICycleCountReleaseDA CycleCountReleaseDA = DataAccess.CreateCycleCountReleaseDA();
        private static readonly ICycleCountRequestDA CycleCountRequestDA = DataAccess.CreateCycleCountRequestDA();
        private static readonly ICycleCountTagDA CycleCountTagDA = DataAccess.CreateCycleCountTagDA();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;

        public CycleCountCriteriaBLL()
        {
            this.pubParasInfo = null;
        }

        public CycleCountCriteriaBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public CycleCountCriteriaBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string CycleCountRelease(CycleCountCriteriaInfo cycleCountCriteriaInfo)
        {
            string strValue = string.Empty;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                strValue = this.SelectInventoryByCycleCountRequest(dataBase, tran, cycleCountCriteriaInfo, whLoginID);
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strValue;
        }

        public string GetCriteriaIDByCycleCountCriteria(DataBase dataBase, DbTransaction tran, CycleCountCriteriaInfo cycleCountCriteriaInfo)
        {
            string whLoginID = this.dataBaseID[0];
            string strCriteriaID = string.Empty;
            if (cycleCountCriteriaInfo != null)
            {
                strCriteriaID = CycleCountCriteriaDA.CheckCycleCountCriteriaUnique(dataBase, tran, cycleCountCriteriaInfo, whLoginID);
                if (!string.IsNullOrEmpty(strCriteriaID))
                {
                    return strCriteriaID;
                }
                strCriteriaID = commonMethod.GenNextNumStr(dataBase, tran, "CYCLE_COUNT_CRITERIA", whLoginID);
                cycleCountCriteriaInfo.CcCriteriaID = strCriteriaID;
                cycleCountCriteriaInfo.CreatedBy = this.pubParasInfo.UserID;
                cycleCountCriteriaInfo.UpdatedBy = cycleCountCriteriaInfo.CreatedBy;
                if (CycleCountCriteriaDA.InsertCycleCountCriteria(dataBase, tran, cycleCountCriteriaInfo, whLoginID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<CycleCountCriteriaInfo>(dataBase, tran, null, cycleCountCriteriaInfo, AuditAction.Insert, this.pubParasInfo.UserID, "CYCLE_COUNT_CRITERIA", whLoginID);
                }
            }
            return strCriteriaID;
        }

        public string SelectInventoryByCycleCountRequest(DataBase dataBase, DbTransaction tran, CycleCountCriteriaInfo cycleCountCriteriaInfo, string whLoginID)
        {
            int num = 1;
            string strValue = string.Empty;
            if (!(cycleCountCriteriaInfo.RequestOnly.ToUpper() == "Y"))
            {
                return strValue;
            }
            List<CycleCountRequestInfo> cycleCountRequestList = CycleCountRequestDA.GetCycleCountRequestByCycleCountCriteria(dataBase, tran, cycleCountCriteriaInfo, whLoginID);
            if ((cycleCountRequestList != null) && (cycleCountRequestList.Count > 0))
            {
                foreach (CycleCountRequestInfo info in cycleCountRequestList)
                {
                    List<string> Sqlwhere = new List<string>();
                    if (!string.IsNullOrEmpty(info.OwnerIdStart))
                    {
                        Sqlwhere.Add(" INV.OWNER_ID >= '" + info.OwnerIdStart + "'");
                    }
                    if (!string.IsNullOrEmpty(info.OwnerIdEnd))
                    {
                        Sqlwhere.Add(" INV.OWNER_ID <= '" + info.OwnerIdEnd + "'");
                    }
                    if (!string.IsNullOrEmpty(info.SkuIdStart))
                    {
                        Sqlwhere.Add(" INV.SKU_ID >= '" + info.SkuIdStart + "'");
                    }
                    if (!string.IsNullOrEmpty(info.SkuIdEnd))
                    {
                        Sqlwhere.Add(" INV.SKU_ID <= '" + info.SkuIdEnd + "'");
                    }
                    if (!string.IsNullOrEmpty(info.LocStart))
                    {
                        Sqlwhere.Add(" INV.LOC >= '" + info.LocStart + "'");
                    }
                    if (!string.IsNullOrEmpty(info.LocEnd))
                    {
                        Sqlwhere.Add(" INV.LOC <= '" + info.LocEnd + "'");
                    }
                    if (info.LocLevelStart != 0)
                    {
                        Sqlwhere.Add(" L.LOC_LEVEL >= " + info.LocLevelStart);
                    }
                    if (info.LocLevelEnd != 0)
                    {
                        Sqlwhere.Add(" L.LOC_LEVEL <= " + info.LocLevelEnd);
                    }
                    if (!string.IsNullOrEmpty(info.ZoneIdStart))
                    {
                        Sqlwhere.Add(" L.ZONE_ID >= '" + info.ZoneIdStart + "'");
                    }
                    if (!string.IsNullOrEmpty(info.ZoneIdEnd))
                    {
                        Sqlwhere.Add(" L.ZONE_ID <= '" + info.ZoneIdEnd + "'");
                    }
                    if (!string.IsNullOrEmpty(info.AreaIdStart))
                    {
                        Sqlwhere.Add(" A.AREA_ID >= '" + info.AreaIdStart + "'");
                    }
                    if (!string.IsNullOrEmpty(info.AreaIdEnd))
                    {
                        Sqlwhere.Add(" A.AREA_ID <= '" + info.AreaIdEnd + "'");
                    }
                    if (cycleCountCriteriaInfo.QtyLessThan > 0M)
                    {
                        Sqlwhere.Add("INV.QTY < " + cycleCountCriteriaInfo.QtyLessThan);
                    }
                    if (cycleCountCriteriaInfo.ShowQty == "N")
                    {
                        Sqlwhere.Add("INV.QTY > 0");
                    }
                    string strCriteriaID = string.Empty;
                    string strReleaseID = string.Empty;
                    List<CycleCountInventoryInfo> InventoryInfo = CycleCountRequestDA.SelectInventoryByCycleRequest(dataBase, tran, Sqlwhere, this.pubParasInfo.UserID, whLoginID);
                    if ((InventoryInfo != null) && (InventoryInfo.Count > 0))
                    {
                        if (string.IsNullOrEmpty(cycleCountCriteriaInfo.CcCriteriaID))
                        {
                            strCriteriaID = this.GetCriteriaIDByCycleCountCriteria(dataBase, tran, cycleCountCriteriaInfo);
                        }
                        else
                        {
                            strCriteriaID = cycleCountCriteriaInfo.CcCriteriaID;
                        }
                        CycleCountReleaseInfo cycleCountReleaseInfo = new CycleCountReleaseInfo {
                            CcCriteriaID = strCriteriaID
                        };
                        strReleaseID = commonMethod.GenNextNumStr(dataBase, tran, "CYCLE_COUNT_RELEASE", whLoginID);
                        strValue = strValue + strReleaseID + ";";
                        cycleCountReleaseInfo.ReleaseID = strReleaseID;
                        cycleCountReleaseInfo.ReleaseDate = DateTime.Now;
                        cycleCountReleaseInfo.CreatedBy = this.pubParasInfo.UserID;
                        cycleCountReleaseInfo.UpdatedBy = cycleCountReleaseInfo.CreatedBy;
                        cycleCountReleaseInfo.Status = CycleCountReleaseStatus.New;
                        CycleCountReleaseDA.InsertCycleCountRelease(dataBase, tran, cycleCountReleaseInfo, whLoginID);
                        try
                        {
                            for (int i = 0; InventoryInfo.Count > i; i++)
                            {
                                CycleCountTagInfo cycleCountTagInfo = new CycleCountTagInfo {
                                    RequestID = info.RequestID
                                };
                                num++;
                                cycleCountTagInfo.TagNo = num;
                                cycleCountTagInfo.OwnerID = InventoryInfo[i].OwnerID;
                                cycleCountTagInfo.SkuID = InventoryInfo[i].SkuID;
                                cycleCountTagInfo.Lot = InventoryInfo[i].Lot;
                                cycleCountTagInfo.Loc = InventoryInfo[i].Loc;
                                cycleCountTagInfo.LpnID = InventoryInfo[i].LpnID;
                                cycleCountTagInfo.SysQty = InventoryInfo[i].Qty;
                                cycleCountTagInfo.Status = info.Status;
                                cycleCountTagInfo.ReleaseID = strReleaseID;
                                cycleCountTagInfo.StatusMsg = "";
                                cycleCountTagInfo.CreatedBy = this.pubParasInfo.UserID;
                                cycleCountTagInfo.CreatedDate = DateTime.Now;
                                cycleCountTagInfo.UpdatedBy = cycleCountTagInfo.CreatedBy;
                                cycleCountTagInfo.UpdatedDate = cycleCountTagInfo.UpdatedDate;
                                CycleCountTagDA.InsertCycleCountTag(dataBase, tran, cycleCountTagInfo, whLoginID);
                            }
                        }
                        catch (Exception ex)
                        {
                            CommonErrorHandler.ErrorHandler(ex, LogType.Error);
                        }
                    }
                    else
                    {
                        strValue = PublicMethod.GetInstance().GetGlobalMsgResource("F2620_002", this.pubParasInfo.UserLanguage);
                    }
                }
                return strValue;
            }
            return PublicMethod.GetInstance().GetGlobalMsgResource("F2620_003", this.pubParasInfo.UserLanguage);
        }

        public string SetCycleCountCriteria(CycleCountCriteriaInfo cycleCountCriteriaInfo)
        {
            string strCriteriaID = string.Empty;
            string whLoginID = this.dataBaseID[0];
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                strCriteriaID = this.GetCriteriaIDByCycleCountCriteria(dataBase, tran, cycleCountCriteriaInfo);
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return strCriteriaID;
        }
    }
}

